<template>
	<!--pages/Information/Information.wxml-->
	<view class="main-box">
		<view class="distribution-box">
			<text>配送方式</text>
			<view class="way-box" :style="'justify-content:' + (distribution_way.length==2?'space-between':'flex-end')">
				<view @tap="chooseway" :data-index="item.index" class='type-flex'
					v-for="(item, index) in distribution_way" :key="index">
					<text :style="'color:' + (delivery_type==item.index?'#333333':'#999999')">{{item.name}}</text>
					<image
						:src="delivery_type==item.index?'../../../images/turntable/circle-sle.png':'../../../images/turntable/circle-nosle.png'"
						class="circle-ico"></image>
				</view>
			</view>
		</view>
		<!-- 商家配送 -->
		<view v-if="delivery_type==1">
			<view class="choose-address-box">
				<view class='address-font-size'>请选择收货地址</view>
				<view class="location-box" @tap="chooseaddress">
					<image src="/static/images/turntable/location_fill@2x.png" class="location-ico"></image>
					<view class="location-info">{{address==''?'请选择':address.address}}</view>
					<image src="/static/images/turntable/right.png" class='choose-image'></image>
				</view>
				<view class="userinfo" v-if="address!=''">姓名:{{address.name}}<text
						class='phone-text'>手机号:{{address.phone}}</text> </view>
			</view>
		</view>
		<!-- 到店自提 -->
		<view v-if="delivery_type==2">
			<view class="choose-address-box choose-address-box-style">
				<view class="info-style">自提信息</view>
				<view class="info-item">
					<text>姓名</text>
					<input class="username-style" @input="nameFun" maxlength="7" placeholder-style="color:#cccccc"
						placeholder="请输入姓名" type="text"></input>
				</view>
				<view class="info-item">
					<text>手机号</text>
					<input class="username-style" @input="phoneFun" placeholder-style="color:#cccccc"
						placeholder="请输入手机号" maxlength="11" type="number"></input>
				</view>
				<view class="info-item">
					<text>门店</text>
					<view class="store-color" @tap="chooseshop">{{merchantArr[selectindex].storetag}}
						<image src="/static/images/turntable/right-red.png" class="right-ico"></image>
					</view>
				</view>
				<view class="info-item">
					<text>领取时间</text>
					<view class="store-color">
						{{AwardInfo.receive_auth=='1'||AwardInfo.receive_auth=='0'?'无限制':AwardInfo.receive_start+'-'+AwardInfo.receive_end}}
					</view>
				</view>
				<view class="info-item">
					<text>营业时间</text>
					<view class="store-color">{{merchantArr[selectindex].business_time }}</view>
				</view>
			</view>
		</view>
		<view class="award-box">
			<image :src="AwardInfo.prize_img" class="award-image-style"></image>
			<view class="award">
				<text>{{AwardInfo.prize_name}}</text>
				<text class='award-type-style'>{{AwardInfo.awardtype}}</text>
			</view>
		</view>
		<view class="btn" @tap="receive">领取</view>
		<!-- 选择门店 -->
		<view class="show-box" v-if="isShow" catchtouchmove="true">
			<view class="popup-main">
				<view class="popup-item" @tap="select" :data-index="index" v-for="(shop, index) in merchantArr"
					:key="index">{{shop.storetag}} （距您{{shop.distance}}）<image class="select-image-style"
						:src="selectindex==index?'../../../images/turntable/circle-sle.png':'../../../images/turntable/circle-nosle.png'">
					</image>
				</view>
				<view class="popup-btn" @tap="sure">确定</view>
			</view>
		</view>
		<!-- 领取成功 -->
		<view class="getsuccess" v-if="success">
			<view class="getsuccess-ico-bg">
				<image src="/static/images/turntable/get.png" class="getsuccess-ico"></image>
			</view>
			<view class="getsuccess-text">领取成功</view>
			<view class="getsuccess-text2">商家会尽快安排发货</view>
			<view class="getsuccess-btn-box">
				<view class="returnindex" @tap="returnindex">返回首页</view>
				<view class="showorder" @tap="showorder">查看订单</view>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/Information/Information.js
	var app = getApp().globalData;

	export default {
		data() {
			return {
				distribution_way: [],
				delivery_type: 1,
				isShow: false,
				merchantArr: [],
				selectindex: 0,
				AwardInfo: {},
				address: '',
				name: '',
				phone: '',
				success: false,
				id: 0,
				prize_id: 0
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		async onLoad(options) {
			await this.$onLaunched;
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			this.setData({
				id: options.id,
				prize_id: options.prize_id
			});
			getApp().globalData.sendRequest({
				url: '/Turntable/getPrizeDeliveryType',
				type: "post",
				data: {
					prize_id: options.prize_id
				},
				success: res => {
					if (res.code == 0) {
						this.AwardInfo = res.data;
						var delivery_type = '';

						switch (this.AwardInfo.delivery_type) {
							case '0':
								this.distribution_way = [{
									name: '商家配送',
									index: 1
								}];
								delivery_type = 1;
								break;

							case '1':
								this.distribution_way = [{
									name: '商家配送',
									index: 1
								}];
								delivery_type = 1;
								break;

							case '2':
								this.distribution_way = [{
									name: '到店自提',
									index: 2
								}];
								delivery_type = 2;
								break;

							case '3':
								this.distribution_way = [{
									name: '商家配送',
									index: 1
								}, {
									name: '到店自提',
									index: 2
								}];
								delivery_type = 1;
								break;
						}

						switch (this.AwardInfo.type) {
							case '1':
								this.AwardInfo.awardtype = '实物';
								break;

							case '2':
								this.AwardInfo.awardtype = '门店商品';
								break;

							case '3':
								this.AwardInfo.awardtype = this.$store.integral_name;
								break;

							case '4':
								this.AwardInfo.awardtype = '余额';
								break;
						}

						this.setData({
							distribution_way: this.distribution_way,
							AwardInfo: this.AwardInfo,
							delivery_type: delivery_type
						});
					}
				}
			}); // 获取自提门店

			uni.getLocation({
				// #ifdef MP-WEIXIN
				type: 'wgs84',
				// #endif
				success: res => {
					getApp().globalData.sendRequest({
						url: '/Turntable/getDeliveryStore',
						type: "post",
						data: {
							win_id: options.id,
							latitude: res.latitude,
							longitude: res.longitude
						},
						success: res => {
							if (res.code == 0) {
								this.merchantArr = res.data;
								this.merchantArr.forEach(res => {
									var distance = res.juli / 1000;
									res.distance = distance.toFixed(1) + 'km';
								});
								this.setData({
									merchantArr: this.merchantArr
								});
							}
						}
					});
				}
			});
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			var value = uni.getStorageSync('shoppingAddress');

			if (value) {
				this.setData({
					address: value
				});
			}

			console.log(this.address);
		},
		
		methods: {
			chooseway(e) {
				this.setData({
					delivery_type: e.currentTarget.dataset.index
				});
			},

			chooseshop() {
				this.setData({
					isShow: true
				});
			},

			select(e) {
				this.setData({
					selectindex: e.currentTarget.dataset.index
				});
			},

			sure() {
				this.setData({
					isShow: false
				});
			},

			chooseaddress() {
				uni.navigateTo({
					url: '../../mine/MyAddress/MyAddress?type=1'
				});
			},

			nameFun(e) {
				this.setData({
					name: e.detail.value
				});
			},

			phoneFun(e) {
				this.setData({
					phone: e.detail.value
				});
			},

			// 领取
			receive() {
				var delivery_type = this.delivery_type;

				if (delivery_type == 1) {
					var address = this.address.address;
				} else {
					var address = '';
				}

				if (delivery_type == 2) {
					if (this.name == '') {
						uni.showToast({
							title: '姓名不能为空',
							duration: 1500,
							icon: 'none'
						});
						return false;
					}

					if (this.phone.length != 11) {
						uni.showToast({
							title: '请输入正确手机号',
							duration: 1500,
							icon: 'none'
						});
						return false;
					}
				}

				getApp().globalData.sendRequest({
					url: '/Turntable/updateReceiveInfo',
					type: "post",
					data: {
						win_id: this.id,
						prize_id: this.prize_id,
						store_id: delivery_type == 1 ? '' : this.merchantArr[this.selectindex].id,
						username: delivery_type == 1 ? this.address.name : this.name,
						phone: delivery_type == 1 ? this.address.phone : this.phone,
						address: address,
						delivery_type: delivery_type,
						prize_type: this.AwardInfo.type
					},
					success: res => {
						if (res.code == 0) {
							this.setData({
								success: true
							});
						} else {
							uni.showToast({
								title: res.msg,
								duration: 1500,
								icon: 'none'
							});
						}
					}
				});
			},

			returnindex() {
				uni.reLaunch({
					url: '/pages/store/store'
				});
			},

			showorder() {
				uni.navigateTo({
					url: '../OrderDetail/OrderDetail?delivery_type=' + this.delivery_type + '&id=' + this.id
				});
			}

		}
	};
</script>
<style>
	@import "./Information.css";
</style>
